DIRECTIONS = {d: i for (i, d) in enumerate(["N", "E", "S", "W"])}
MOVE = [(0, -1), (1, 0), (0, 1), (-1, 0)]
ROTX = [(1, 0), (0, -1), (-1, 0), (0, 1)]
ROTY = [(0, 1), (1, 0), (0, -1), (-1, 0)]

with open('input.txt') as file:
    x, y, d = 0, 0, DIRECTIONS["E"]
    wpx, wpy = 10, -1
    
    for line in file.readlines():
        op, arg = line[0], int(line[1:])

        if op in DIRECTIONS:
            dx, dy = MOVE[DIRECTIONS[op]]
            wpx += dx * arg
            wpy += dy * arg
        elif op == "F":
            x += wpx * arg
            y += wpy * arg
        else:
            rot = arg // 90 if op == "R" else ((-arg) % 360) // 90
            rotx, roty = ROTX[rot], ROTY[rot]
            wpx, wpy = wpx * rotx[0] + wpy * rotx[1], wpx * roty[0] + wpy 

    print(abs(x) + abs(y))